﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNet.WinUI.Admin;
using DingY.DB;

namespace DotNet.WinUI.Customer
{
    public partial class FrmMain : BaseForm
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private string _LoginID;
        public string LoginID
        {
            get { return _LoginID; }
            set { _LoginID = value; }
        }

        private void tsMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "用户管理":
                    FrmUsersList objFrmUsersList = new FrmUsersList();
                    objFrmUsersList.ShowDialog();
                    break;
                case "重新登录":
                    LoginID = "";
                    ShowLoginDlg();
                    break;
                case "修改密码":
                    FrmModPwd objFrmModPwd = new FrmModPwd(LoginID);
                    objFrmModPwd.ShowDialog();
                    break;
                case "退出系统":
                    Close();
                    break;
                default:
                    ShowMsg("此功能暂未实现！");
                    break;
                case "行业信息":
                    FrmIndustryList objFrmIndustryList = new FrmIndustryList();
                    objFrmIndustryList.ShowDialog();
                    break;
                case "客户档案":
                    FrmCustomerEdit objFrmCustomerEdit = new FrmCustomerEdit();
                    objFrmCustomerEdit.ShowDialog();
                    LoadCustomer();
                    break;
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            ShowLoginDlg();
        }

        private void ShowLoginDlg()
        {
            FrmLogin objFrmLogin = new FrmLogin();
            if (objFrmLogin.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                Close();

                return;
            }
            LoginID = objFrmLogin._LoginID;
            stbLoingInfo.Text = string.Format("  欢迎 {0} 【 {1} 】 登录系统，登录时间：{2}", LoginID, GetUsersName(LoginID), DateTime.Now);

            //加载基础数据
            FillIndustry(cmbIndustry, "全部");
            cmbDuties.SelectedIndex = 0;
            LoadCustomer();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ShowQuestionMsg("确认退出系统?") != DialogResult.OK)
            {
                e.Cancel = true;
                if (string.IsNullOrEmpty(LoginID))
                {
                    ShowLoginDlg();
                }
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {

            LoadCustomer();
        }

        public void LoadCustomer()
        {
            string _Sql = @"SELECT Customer.CustomerID AS 编号, Customer.Name AS 客户名称,
	                Datename(Month, Customer.Birthday) + '月' + Datename(DAY, Customer.Birthday) + '日' AS 生日,
	                --(case Customer.Duties when 1 then '总经理' when 2 then '副总经理' when 3 then '部门经理' when 4 then '职员' else '未定义' end) AS 职务, 
Customer.Duties AS 职务, 
		                Industry.Name AS 行业 , Customer.Mobile AS 手机号码, Customer.CompanyName AS 所在公司
                FROM Customer, Industry
                WHERE Industry.IndustryID = Customer.IndustryID
                    and Customer.Name like '%{0}%'
                    and Customer.Mobile like '%{1}%' ";
            _Sql = string.Format(_Sql, txtName.Text, txtMobile.Text);

            if (cmbDuties.SelectedIndex >= 1)
            {
                _Sql += string.Format(" and Customer.Duties = {0} ", cmbDuties.SelectedIndex);
            }

            if (!string.Equals(cmbIndustry.SelectedValue.ToString(), "0"))
            {
                _Sql += string.Format(" and Customer.IndustryID = {0} ", cmbIndustry.SelectedValue);
            }

            FillDataToGridView(dgData, _Sql);
            dgData.Columns[0].Visible = false;
            dgData.Columns[1].HeaderText = "客户名称";
        }

        private void dgData_DoubleClick(object sender, EventArgs e)
        {

            if (dgData.SelectedRows.Count <= 0)
            {
                ShowMsg("未选择数据信息！");
                return;
            }

            if (dgData.SelectedCells[0].Value.ToString() == "")
            {
                ShowMsg("未选择有效数据信息！");
                return;
            }

            string _CustomerID = dgData.SelectedCells[0].Value.ToString();
            if (string.IsNullOrEmpty(_CustomerID))
            {
                ShowMsg("没有选择有效的客户档案信息！");
                return;
            }
            (new FrmCustomerEdit("Modify", _CustomerID)).ShowDialog();
            LoadCustomer();
        }

        private void mnCustomer_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "新增":
                    (new FrmCustomerEdit()).ShowDialog();
                    break;
                case "编辑":
                    dgData_DoubleClick(sender, e);
                    break;
                case "删除":
                    DelCustomer();
                    break;
                case "查看":
                    if (dgData.SelectedRows.Count <= 0)
                    {
                        ShowMsg("未选择数据信息！");
                        return;
                    }

                    string _CustomerID = dgData.SelectedCells[0].Value.ToString();
                    if (string.IsNullOrEmpty(_CustomerID))
                    {
                        ShowMsg("没有选择有效的客户档案信息！");
                        return;
                    }
                    (new FrmCustomerEdit("Find", _CustomerID)).ShowDialog();
                    break;
            }
            LoadCustomer();
        }

        private void DelCustomer()
        {
            if (dgData.SelectedRows.Count <= 0)
            {
                ShowMsg("未选择数据信息！");
                return;
            }

            if (dgData.SelectedCells[0].Value.ToString() == "")
            {
                ShowMsg("未选择有效数据信息！");
                return;
            }

            if (ShowQuestionMsg(string.Format("确认删除编码为【 {0} 】的数据吗", dgData.SelectedCells[1].Value.ToString())) == DialogResult.OK)
            {
                string sql = @"DELETE FROM Customer WHERE CustomerID = '{0}' ";
                sql = string.Format(sql, dgData.SelectedCells[0].Value.ToString());

                SqlDB objSqlDB = new SqlDB();
                int i = objSqlDB.ExecuteSql(sql);
                if (i >= 1)
                {
                    ShowMsg("成功删除数据信息！");
                }
            }
        }

        private void dgData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgData.Columns[e.ColumnIndex].DataPropertyName == "职务")
            {
                switch (Convert.ToInt32(dgData.Rows[e.RowIndex].Cells["职务"].Value))
                {
                    case 1:
                        e.Value = "总经理";
                        break;
                    case 2:
                        e.Value = "副总经理";
                        break;
                    case 3:
                        e.Value = "部门经理";
                        break;
                    default:
                        break;
                }
                e.FormattingApplied = true;
            }
        }
    }
}
